angular.module('app').config(function ($stateProvider) {

    $stateProvider.state('app.globalization', {
        url         : '/globalization',
        label       : '语言环境&特定操作',
        controller  : 'app.globalization',
        controllerAs: '$ctrl',
        templateUrl : 'view/app/globalization/globalization.html'
    });
}).controller('app.globalization', function ($ionicPlatform, $timeout) {
    var ctrl = this;
    /**
     * GlobalizationError
     * - code [GlobalizationError.UNKNOWN_ERROR(code 0), GlobalizationError.FORMATTING_ERROR(code 1), GlobalizationError.PARSING_ERROR(code 2), GlobalizationError.PATTERN_ERROR(code 3)]
     * - message
     */
    $ionicPlatform.ready(function () {
        /**
         * navigator.globalization.getPreferredLanguage(successCallback, errorCallback);
         */
        navigator.globalization.getPreferredLanguage(function (language) {
            $timeout(function () {
                ctrl.preferredLanguage = language;
            });
        }, onError);

        /**
         * navigator.globalization.getLocaleName(successCallback, errorCallback);
         */
        navigator.globalization.getLocaleName(function (locale) {
            $timeout(function () {
                ctrl.locale = locale;
            });
        }, onError);

        /**
         * navigator.globalization.dateToString(date, successCallback, errorCallback, options);
         * - options
         *   - formatLength: [short, medium, long, full]
         *   - selector: [date, time, date and time]
         */
        navigator.globalization.dateToString(new Date(), function (dateString) {
            $timeout(function () {
                ctrl.now = dateString;
            });
        }, onError, {formatLength: 'short', selector: 'date and time'});

        /**
         * navigator.globalization.getCurrencyPattern(currencyCode, successCallback, errorCallback);
         * - currencyCode: 货币格式, 如: USD
         * - successCallback: 会传入一个 {pattern, code, fraction, rounding, decimal, grouping} 对象的参数
         */
        navigator.globalization.getCurrencyPattern('USD', function (result) {
            $timeout(function () {
                ctrl.currencyPattern = result;
            });
        }, onError);

        /**
         * navigator.globalization.getDateNames(successCallback, errorCallback, options);
         * - options
         *   - type: [narrow, wide]
         *   - item: [months, days]
         */
        navigator.globalization.getDateNames(function (names) {
            $timeout(function () {
                ctrl.names = names;
            });
        }, onError, {type: 'wide', item: 'months'});

        /**
         * navigator.globalization.getDatePattern(successCallback, errorCallback, options);
         * - successCallback: 会传入一个 {pattern, timezone, utc_offset, dst_offset} 对象的参数
         * - options
         *   - formatLength: [short, medium, long, full]
         *   - selector: [date, time, date and time]
         */
        navigator.globalization.getDatePattern(function (result) {
            $timeout(function () {
                ctrl.datePattern = result;
            });
        }, onError, {formatLength: 'short', selector: 'date and time'});

        /**
         * navigator.globalization.getFirstDayOfWeek(successCallback, errorCallback);
         * - successCallback: 会传入一个 {value} 对象的参数
         */
        navigator.globalization.getFirstDayOfWeek(function (result) {
            $timeout(function () {
                ctrl.firstDayOfWeek = result.value;
            });
        }, onError);

        /**
         * navigator.globalization.getNumberPattern(successCallback, errorCallback, options);
         * - successCallback: 会传入一个 {pattern, symbol, fraction, rounding, positive, negative, decimal, grouping} 对象的参数
         * - options
         *   - type: [decimal, percent, currency]
         */
        navigator.globalization.getNumberPattern(function (result) {
            $timeout(function () {
                ctrl.numberPattern = result;
            });
        }, onError, {type: 'decimal'});

        /**
         * navigator.globalization.isDayLightSavingsTime(date, successCallback, errorCallback);
         * - successCallback: 会传入一个 {dst} 对象的参数, dst = true 时说明给定的 date 是夏令时
         */
        navigator.globalization.isDayLightSavingsTime(new Date(), function (result) {
            $timeout(function () {
                ctrl.isDayLight = result.dst;
            });
        }, onError);

        /**
         * navigator.globalization.numberToString(number, successCallback, errorCallback, options);
         * - options
         *   - type: [decimal, percent, currency]
         */
        navigator.globalization.numberToString(123456.34567, function (result) {
            $timeout(function () {
                ctrl.numStr = result.value;
            });
        }, onError, {type: 'decimal'});

        /**
         * navigator.globalization.stringToDate(dateString, successCallback, errorCallback, options);
         * - successCallback: 会传入一个 {year, month, day, hour, minute, second, millisecond} 对象的参数
         * - options
         *   - formatLength: [short, medium, long, full]
         *   - selector: [date, time, date and time]
         */
        navigator.globalization.stringToDate('9/25/2012', function (result) {
            $timeout(function () {
                ctrl.str2Date = result;
            });
        }, onError, {selector: 'date'});

        /**
         * navigator.globalization.stringToNumber(string, successCallback, errorCallback, options);
         * - successCallback: 会传入一个 {year, month, day, hour, minute, second, millisecond} 对象的参数
         * - options
         *   - type: [decimal, percent, currency]
         */
        navigator.globalization.stringToNumber('1234.56', function (result) {
            $timeout(function () {
                ctrl.str2Num = result.value;
            });
        }, onError, {formatLength: 'short', selector: 'date and time'});
    });

    function onError(GlobalizationError) {
        alert('onError - ' + JSON.stringify(GlobalizationError));
    }
});